#include <sys/types.h>         
#include <sys/socket.h>
#include <sys/un.h>
#include <errno.h>
#include <signal.h>
#include <unistd.h>

#include "ipc_api.h"

typedef struct {
	int			running;
}lockServer_s;
lockServer_s	lockServer = {1};
lockServer_s	*pLockServer = &lockServer;

void signalRountine(int signNo)
{
	switch(signNo)
	{
		case SIGINT://ctrl+c
			pLockServer->running = 0;
			break;
		case SIGTERM://kill
			pLockServer->running = 0;
			break;
	}
}

int main(int argc,char *argv[])  
{
	ipcRet_e	ret = ipcSuccess;
	ipcLog_s	log = {IPC_LOG_DEBUG,IPC_LOG_2_STDERR};
	int 		create	 = TRUE ;

	signal(SIGINT,signalRountine);
	signal(SIGTERM,signalRountine);
	
	ret = ipcLogInit(&log);
	ipcLogCheckRetThenReturn(ret);

	ipcShareMemoryInit(create);
	ipcLogCheckRetThenReturn(ret);
	
	ipcLockInit(create);
	ipcLogCheckRetThenReturn(ret);

	while(pLockServer->running)
	{
		int sleepTime = random()%SLEEP_TIME_RANDOM_MOD;
		ipcLockContext_s context ={getpid(),(char*)"server"};
		
		ipcLogDebug("pid %d sleepTimeUs %d \n",getpid(),sleepTime);
		
		ret = ipcLock(&context);
		if(ipcSuccess == ret)
		{
			ipcUnlock(&context);
		}

		sleep(sleepTime);

	}

	ipcLockDeinit(create);
	ipcShareMemoryDeinit(create);
	ipcLogDeinit(); 

	return 0;
}

